﻿using Microsoft.AspNetCore.Mvc;
using Net5ApiAndEfMySql.API.Controllers;
using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.DtoModel.SystemAdmin;
using Net5ApiAndEfMySql.IService.SystemAdmin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace VueAndNetCoreEfApi.Controllers
{
    public class OperatorController : BaseApiController
    {
        private IOperatorService _iOperatorService;
        public OperatorController(IOperatorService iOperatorService)
        {
            _iOperatorService = iOperatorService;
        }

        //操作员显示
        [HttpPost]
        public ResultModel GetPageList(SearchModel m)
        {
            ResultModel result = new ResultModel();
            if (m == null)
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _iOperatorService.GetPageList(m);

                data.draw = m.draw;
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;

        }

        //操作员添加
        public ResultModel AddOperator(OperatorDto obj)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(obj.op_account))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入账号";
                return result;
            }
            if (string.IsNullOrWhiteSpace(obj.op_card))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入编号";
                return result;
            }
            try
            {

                var resultData = _iOperatorService.AddOperator(obj);
                if (resultData)
                {
                    result.code = ResultCode.Ok;
                    result.data = resultData;
                    result.mess = "成功!";
                }
                else
                {
                    result.code = ResultCode.Fail;
                    result.data = resultData;
                    result.mess = "添加失败!";
                }

            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }

        //操作员修改
        [HttpPost]
        public ResultModel UptOperator(OperatorDto obj)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(obj.op_account))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入账号";
                return result;
            }
            if (string.IsNullOrWhiteSpace(obj.op_card))
            {
                result.code = ResultCode.Error;
                result.mess = "必须输入编号";
                return result;
            }
            try
            {
                var resultData = _iOperatorService.UptOperator(obj);
                result.code = ResultCode.Ok;
                result.data = resultData;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }

        //操作员状态修改
        public ResultModel UptStateByCard(string card)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(card))
            {
                result.code = ResultCode.Error;
                result.data = false;
                result.mess = "参数错误";
                return result;
            }
            if (_iOperatorService.UptStateByCard(card))
            {
                result.code = ResultCode.Ok;
                result.data = true;
                result.mess = "成功";
            }
            else
            {
                result.code = ResultCode.Ok;
                result.data = false;
                result.mess = "失败";
            }
            return result;
        }

        //操作员删除
        public ResultModel DelOperator(string card)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(card))
            {
                result.code = ResultCode.Error;
                result.data = false;
                result.mess = "参数错误";
                return result;
            }
            if (_iOperatorService.DelOperator(card))
            {
                result.code = ResultCode.Ok;
                result.data = true;
                result.mess = "删除成功";
            }
            else
            {
                result.code = ResultCode.Ok;
                result.data = false;
                result.mess = "删除失败";
            }
            return result;
        }

        //操作员批量删除
        public ResultModel DelOperatorList(string keys)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(keys))
            {
                result.code = ResultCode.Error;
                result.data = false;
                result.mess = "参数错误";
                return result;
            }
            if (_iOperatorService.DelOperatorList(keys))
            {
                result.code = ResultCode.Ok;
                result.data = true;
                result.mess = "删除成功";
            }
            else
            {
                result.code = ResultCode.Ok;
                result.data = false;
                result.mess = "删除失败";
            }
            return result;
        }

        //操作员获取
        public ResultModel GetOperatorByCard(string card)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrWhiteSpace(card))
            {
                result.code = ResultCode.Error;
                result.mess = "参数错误";
                return result;
            }
            try
            {
                var data = _iOperatorService.GetOperatorByCard(card);
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }

        //唯一验证
        public ResultModel ValidateOperatorByAccount(string account)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrEmpty(account))
            {
                result.code = ResultCode.Error;
                result.data = false;
                result.mess = "参数错误";
                return result;
            }
            if (_iOperatorService.ValidateOperatorByAccount(account))
            {
                result.code = ResultCode.Ok;
                result.data = true;
                result.mess = "账号可以使用";
            }
            else
            {
                result.code = ResultCode.Ok;
                result.data = false;
                result.mess = "账号已经被使用";
            }
            return result;

        }
        public ResultModel ValidateOperatorByCard(string card)
        {
            ResultModel result = new ResultModel();
            if (string.IsNullOrEmpty(card))
            {
                result.code = ResultCode.Error;
                result.data = false;
                result.mess = "参数错误";
                return result;
            }
            if (_iOperatorService.ValidateOperatorByCard(card))
            {
                result.code = ResultCode.Ok;
                result.data = true;
                result.mess = "编号可以使用";
            }
            else
            {
                result.code = ResultCode.Ok;
                result.data = false;
                result.mess = "编号已经被使用";
            }
            return result;

        }

        //获取角色
        public ResultModel GetRoles()
        {
            ResultModel result = new ResultModel();
            try
            {
                var data = _iOperatorService.GetRoles();
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
        //获取员工
        public ResultModel GetEmployees()
        {
            ResultModel result = new ResultModel();
            try
            {
                var data = _iOperatorService.GetEmployees();
                result.code = ResultCode.Ok;
                result.data = data;
                result.mess = "成功!";
            }
            catch (Exception ex)
            {
                result.code = ResultCode.Error;
                result.mess = "错误!";
            }
            return result;
        }
    }
}
